import { Plugin } from 'vite'
import { mockRoutes } from '../mock/routes'

export const createMockServer = (): Plugin => {
  return {
    name: 'vite-mock-server',
    configureServer(server) {
      mockRoutes.forEach((route: any) => {
        server.middlewares.use(route.url, async (req, res, next) => {
          if (route.method && req.method !== route.method) {
            return next()
          }
          route.handler(req, res)
        })
      })
    }
  }
}
